#include "TileForwardLightCullCSFX.h"

TileForwardLightCullCSFX::TileForwardLightCullCSFX(ID3D11Device* device)
	: AbstractComputeFX(device, L"FX/TileForwardLightCullCS.fxo") 
{
	//Call init functions
	InitShaderVariablePointers();
}

TileForwardLightCullCSFX::~TileForwardLightCullCSFX()
{}

void TileForwardLightCullCSFX::InitShaderVariablePointers()
{
	//Input/Output data (SRV/UAV)
	zPrePassBufferVar = effect->GetVariableByName("ZPrePassDepthBuffer")->AsShaderResource();
	zPrePassBufferMSAAVar = effect->GetVariableByName("ZPrePassDepthBufferMSAA")->AsShaderResource();
	llbVar = effect->GetVariableByName("LightListBuffer")->AsShaderResource();

	llibVar = effect->GetVariableByName("LightListIndexBuffer")->AsUnorderedAccessView();
	llsebVar = effect->GetVariableByName("LightListStartEndBuffer")->AsUnorderedAccessView();

	maxZTileBuffVar = effect->GetVariableByName("MaxZTileBuffer")->AsUnorderedAccessView();

	//On resize event CB
	renderTargetWidthVar = effect->GetVariableByName("rtWidth")->AsScalar();
	renderTargetHeightVar = effect->GetVariableByName("rtHeight")->AsScalar();

	//Per frame CB
	invProjectionVar = effect->GetVariableByName("invProjection")->AsMatrix();
	viewVar = effect->GetVariableByName("view")->AsMatrix();

	//Light list data
	numLightVar = effect->GetVariableByName("totalNumLights")->AsScalar();

	//Lighting data for transparant tiles
	llibtVar = effect->GetVariableByName("LightListIndexBufferT")->AsUnorderedAccessView();
	llsebtVar = effect->GetVariableByName("LightListStartEndBufferT")->AsUnorderedAccessView();
}

void TileForwardLightCullCSFX::SetOnResizeCBuffer(float newWidth, float newHeight)
{
	renderTargetWidthVar->SetFloat(newWidth); 
	renderTargetHeightVar->SetFloat(newHeight);
}

void TileForwardLightCullCSFX::SetPerFrameCBuffer(CXMMATRIX& invProj, CXMMATRIX& view)
{
	invProjectionVar->SetMatrix(reinterpret_cast<const float*>(&invProj));
	viewVar->SetMatrix(reinterpret_cast<const float*>(&view));
}

void TileForwardLightCullCSFX::SetLightData(unsigned lightCount)
{
	numLightVar->SetInt(lightCount);
}

void TileForwardLightCullCSFX::SetInputDepthTexture(ID3D11ShaderResourceView* depthTex, bool isMSAATex)
{
	//MSAA texture
	if (isMSAATex)
	{
		zPrePassBufferMSAAVar->SetResource(depthTex);
		zPrePassBufferVar->SetResource(NULL);
	}
	else
	{
		zPrePassBufferVar->SetResource(depthTex);
		zPrePassBufferMSAAVar->SetResource(NULL);
	}
}

void TileForwardLightCullCSFX::SetLightListBuffer(ID3D11ShaderResourceView* llb)
{
	llbVar->SetResource(llb);
}

void TileForwardLightCullCSFX::SetLightListIndexBufferOutput(ID3D11UnorderedAccessView* llibOut)
{
	llibVar->SetUnorderedAccessView(llibOut);
}

void TileForwardLightCullCSFX::SetLightListStartEndBufferOutput(ID3D11UnorderedAccessView* llsebOut)
{
	llsebVar->SetUnorderedAccessView(llsebOut);
}

void TileForwardLightCullCSFX::SetMaxZTileBuffer(ID3D11UnorderedAccessView* maxZTileBuffer)
{
	maxZTileBuffVar->SetUnorderedAccessView(maxZTileBuffer);
}

void TileForwardLightCullCSFX::SetLightListIndexBufferTransparantOutput(
	ID3D11UnorderedAccessView* llibtOut)
{
	llibtVar->SetUnorderedAccessView(llibtOut);
}

void TileForwardLightCullCSFX::SetLightListStartEndBufferTransparantOutput(
	ID3D11UnorderedAccessView* llsebtOut)
{
	llsebtVar->SetUnorderedAccessView(llsebtOut);
}